package com.yumijinfu.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yumijinfu.R;
import com.yumijinfu.base.BaseActivity;
import com.yumijinfu.http.HttpCallBack;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.mian.Config;
import com.yumijinfu.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * yihuangxing 帮助中心
 */
public class HelpCenterActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    //
    @Bind(R.id.pb_loading)
    ProgressBar pbLoading;
    @Bind(R.id.mWebView)
    WebView vWVContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_help_center);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {

        titlebar.setTitleBar(R.mipmap.back, "帮助中心", 0);
        titlebar.setTitleBarBackgroundColor(getResources().getColor(R.color.white));
        titlebar.setTitleBarTextColor(getResources().getColor(R.color.black));

    }

    @Override
    protected void initView() {

        //初始化WebView
        vWVContent.setWebViewClient(new WebViewClient());
        vWVContent.getSettings().setJavaScriptEnabled(true);
        vWVContent.getSettings().setBuiltInZoomControls(true); //显示放大缩小 controler
        vWVContent.getSettings().setSupportZoom(true); //可以缩放
        //屏幕自适应
        vWVContent.getSettings().setUseWideViewPort(true);
        vWVContent.getSettings().setLoadWithOverviewMode(true);
        vWVContent.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pbLoading.setProgress(newProgress);
                if (newProgress == 100) {
                    pbLoading.setVisibility(View.GONE);
                }

            }

        });

        getHelpData();

    }

    @Override
    protected void setData() {

    }

    /**
     * 帮助中心
     */

    private void getHelpData() {

        HttpParams params=new HttpParams();

        HttpUtils.post(Config.HELPER, params, new HttpCallBack(this,"加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if(null!=result){
                    String url = result.optString("url");
                    vWVContent.loadUrl(url);
                }
            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(HelpCenterActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick({R.id.titlebar_iv_back})
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_iv_back:
                finish();

                break;
//            case R.id.lly_cjwt:     //常见问题
//
//                startActivity(new Intent(HelpCenterActivity.this,ProblemActivity.class));
//
//                break;
//            case R.id.lly_fwsm:   //  服务说明
//                startActivity(new Intent(HelpCenterActivity.this,InstructionsActivity.class));
//                break;
//            case R.id.lly_yhxy:  //用户协议
//                startActivity(new Intent(HelpCenterActivity.this,AgreementActivity.class));
//                break;
            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && vWVContent.canGoBack()) {
            vWVContent.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
